<?php
/**
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
function load_properties($component)
{
	$properties = array();
	$temp = NULL;
	foreach ( $component->childNodes as $child)
	{
		if( $child-> tagName != 'property')
		{
			continue;
		}
		$key = $child->getAttribute('key');
		if( $child->hasChildNodes())
		{
			if( $child-> firstChild-> nodeType == XML_TEXT_NODE )
			{
				$value = $child->nodeValue;
			}
			else
			{
				$value = load_properties( $child);
			}
		}
		else
		{
			$value = $child->nodeValue;
			//$value = $child->getAttribute('value');
		}
		$temp = array($key => $value);
		$properties = array_merge ($properties, $temp);
	}
	
	//print_r($properties);
	
	return $properties;
}

function save_properties($component, $properties, $doc)
{
	//remove all the property for avoid unnecessery duplicates and remove the unwanted
	$child =  $component->firstChild;
	while($child != NULL)
	{
		if( $child-> tagName == 'property')
		{
			$temp_child = $child;
			$child = $child->nextSibling;
			$component->removeChild($temp_child);
			continue;
		}
		$child = $child->nextSibling;
	}
	
	$next_child = $component->firstChild;
		
	$keys = array_keys($properties);
	
	//fill with the new properties
	foreach ( $keys as $key)
	{
		$value = $properties[$key];
		$property_ele = $doc-> createElement('property');
		$property_ele-> setAttribute('key', $key);
		if ( !is_array($value) )
		{
			//$property_ele-> setAttribute('value', $value);
			$valueNode = $doc-> createTextNode($value);

			$property_ele->appendChild($valueNode);
		}
		else
		{
			save_properties( $property_ele, $value, $doc);
		}
		
		$component-> insertBefore($property_ele, $next_child);
	}
}


?>